package com.util
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	import xm.align.AlignFactory;
	import xm.components.button.SWCButton;
	import xm.components.calendar.CalendarSpinner;
	import xm.components.checkbox.CheckBox;
	import xm.components.checkbox.CheckGroup;
	import xm.components.radio.RadioButton;
	import xm.components.radio.RadioGroup;
	import xm.components.spinner.Spinner;
	import xm.components.spinner.SpinnerContainer;
	import xm.components.text.TextInput;
	import xm.components.text.TruncateTextField;
	import xm.control.Navigator;
	import xm.control.bar.barContent.ActionContent;
	import xm.control.bar.barContent.NavigationContent;
	import xm.control.bar.barContent.TitleContent;
	import xm.core.XMSprite;
	import xm.definition.Align;
	import xm.definition.Arrange;
	
	/**
	 * @author 강석민
	 */	
	public class MakeChildrenUtil
	{
		
		static public function makeInputTextField( width : Number, height : Number, 
												   textSize : int = 12, textColor : uint = 0x000000, bold : Boolean = false,
												   border : Boolean = true, borderColor : uint = 0x000000, 
												   background : Boolean = true, backgroundColor : uint = 0xffffff ):TextField
		{
			
			var $color : uint 		= textColor;
			var $size : int 		= textSize;
			var $font : String 		= null;
			var $bold : Boolean 	= bold;
			var $textFormat : TextFormat = new TextFormat( $font , $size , $color , $bold );
			
			var tf : TextField = new TextField();
			tf.type 					= TextFieldType.INPUT;
			tf.defaultTextFormat 		= $textFormat;
			tf.width 					= width;
			tf.height 					= height;
			tf.border 					= border;
			tf.borderColor 				= borderColor;
			tf.background 				= background;
			tf.backgroundColor			= backgroundColor;
			
			return tf;
		}
		
		/**
		 * 			일반 텍스트 필드 생성
		 * @param text
		 * @param color
		 * @param size
		 * @param dropShadow
		 * @param autoSize
		 * @return 
		 * 
		 */		
		static public function makeTextField( 
			text : String = "InpuText", color : int = 0x000000, size : int = 24, dropShadow : Boolean = false, autoSize : Boolean = true ):TextField
		{
			var textField : TextField = new TextField();
			
			var $color : uint = color;
			var $size : int = size;
			var $font : String = null;
			var $bold : Boolean = true;
			var $textFormat : TextFormat = new TextFormat( $font , $size , $color , $bold );
			
			textField.defaultTextFormat = $textFormat;
			if( autoSize == true )	textField.autoSize = TextFieldAutoSize.LEFT;
			textField.selectable = false;
			textField.wordWrap = false;
			textField.multiline = false;
//			textField.border = true;//임시
			textField.borderColor = 0xff0000;//임시
			
			if( dropShadow == true )
			{
				var myDropFilter : DropShadowFilter = new DropShadowFilter();
				myDropFilter.alpha =0.75;
				myDropFilter.angle = 45;
				myDropFilter.distance = 2;
				myDropFilter.blurX = 0;
				myDropFilter.blurY = 0;
				myDropFilter.quality = BitmapFilterQuality.HIGH;
				var myFilters:Array = textField.filters;
				myFilters.push(myDropFilter);
				textField.filters = myFilters;
			}
			if( text == null ) text = "";
			textField.text = text;
			
			return textField;
		}
		
		/**
		 * TruncateTextField 텍스트 생성				
		 * @param text
		 * @param color
		 * @param size
		 * @param truncate
		 * @param viewWidth
		 * @param rightCut
		 * @return 
		 * 
		 */		
		static public function makeTrunTextField( 
			text : String = "TrunTextField", color : int = 0x000000, size : int = 24, viewWidth : int = 50 , backCut : Boolean = true ):TruncateTextField
		{
			var textField : TruncateTextField = new TruncateTextField();
			
			var $color : uint = color;
			var $size : int = size;
			var $font : String = null;
			var $bold : Boolean = true;
			var $textFormat : TextFormat = new TextFormat( $font , $size , $color , $bold );
			
			textField.defaultTextFormat = $textFormat;
			textField.autoSize = TextFieldAutoSize.LEFT;
			textField.selectable = false;
			textField.wordWrap = false;
			textField.multiline = false;
//			textField.border = true;//임시
			
			textField.borderColor = ( backCut ) ? 0x00FFFF : 0xFFFF00;//임시
			textField.isTruncateToBack = backCut;
			
			textField.viewWidth = viewWidth;
			textField.textWithTruncate = text;
			
			return textField;
		}
		
		/**
		 * TextInput 생성
		 * @param width						
		 * @param height
		 * @param wordWrap
		 * @param multiline
		 * @param maxChars
		 * @param displayAsPassword
		 * @return 
		 * 
		 */		
		static public function makeTextInput( width : Number, height : Number, textColor : uint = 0x000000, textSize : int = 12, wordWrap : Boolean = false, multiline : Boolean = false
											  , maxChars : int = 0, displayAsPassword : Boolean = false ) : TextInput
		{
			// TextInput
			var inputTF : TextInput = new TextInput();
			inputTF.viewWidth 	= width;
			inputTF.viewHeight 	= height;
			var paddingTop : Number = ( height-textSize )/2;
			var paddingLeft : Number = 3;
			inputTF.padding = [ paddingTop, 0, 0, paddingLeft];
			inputTF.defaultObject = "내용입력";
//			var icon : MovieClip = new ICON_SEARCH;
//			icon.x = 11;
//			icon.y = 15;
//			inputTF.defaultObject = icon;
			inputTF.bgClip = new BG_TEXT_INPUT;
			
			// TextInput.TextField
			var format:TextFormat = new TextFormat();
			format.color 	= textColor;
			format.size 	= textSize;
			
			inputTF.textField.defaultTextFormat 		= format;
			inputTF.textField.wordWrap 					= wordWrap;
			inputTF.textField.multiline  				= multiline;
			inputTF.textField.maxChars 					= maxChars;
			inputTF.textField.displayAsPassword 		= displayAsPassword;
			
			inputTF.invalidateDisplayList();
			
			
			return inputTF;
		}
		
		/**
		 * CUSTOM SWC 버튼 생성
		 * 예제)		MakeChildrenUtil.swcBtn( BtnTypeManager.MAIN_NOTICE , "bg","text" );
		 * @param BtnType			BtnTypeManager 에서 버튼 타입 입력	
		 * @param instansName		버튼 내부의 인스턴스 이름 String 으로 입력
		 * @return 					SWCButton
		 * 
		 */
		static public function makeSwcBtn( BtnType : MovieClip, ...instansName ) : SWCButton
		{
			var btn : SWCButton = new SWCButton();
			btn.skin = BtnType;
			
			var arrInstance : Array = [];
			for (var i : uint = 0; i < instansName.length; i++) 
			{
				arrInstance.push( btn.skin[   instansName[i]    ] as MovieClip );
				btn.target = arrInstance;
			} 
			return btn;
		}
		
		/**
		 * 범용 버튼 생성
		 * 예제)		MakeChildrenUtil.swcBtn( BtnTypeManager.MAIN_NOTICE , "bg","text" );
		 * @param BtnType			BtnTypeManager 에서 버튼 타입 입력	
		 * @param instansName		버튼 내부의 인스턴스 이름 String 으로 입력
		 * @return 					SWCButton
		 * 
		 */		
		static public function makeCommonBtn( 
			text : String, width : Number = 100, height : Number = 50,
			textSize : uint = 20,textAlpha : Number = 1, textColor : uint = 0x000000, bold : Boolean = true, 
			dropShadow : Boolean = false
		) : SWCButton
		{
			var mc : MovieClip = new BTN_COMMON();
			//bg 설정
			var bg : MovieClip = mc.bg;
			bg.width = width;
			bg.height = height;
			
			//txt 설정
			var txt : TextField = MakeChildrenUtil.makeTextField( text , 0x000000, 16 );
			var txtBitmap : Bitmap = BitmapUtil.copyDisplayObject( txt, txt.width, txt.height, true );
			txtBitmap.x = ( bg.width - txtBitmap.width ) /2;
			txtBitmap.y = ( bg.height - txtBitmap.height ) /2;
			mc.addChild( txtBitmap );
			
			if( dropShadow == true )
			{
				var myDropFilter : DropShadowFilter = new DropShadowFilter();
				myDropFilter.alpha =0.75;
				myDropFilter.angle = 45;
				myDropFilter.distance = 2;
				myDropFilter.blurX = 0;
				myDropFilter.blurY = 0;
				myDropFilter.quality = BitmapFilterQuality.HIGH;
				var myFilters : Array = txt.filters;
				myFilters.push(myDropFilter);
				txt.filters = myFilters;
			}
			
			//target 설정
			var btn : SWCButton = new SWCButton();
			btn.skin = mc;
			btn.target = [ btn.skin.bg as MovieClip ];
			
			return btn;
		}
		
		/**
		 * XMSprite 생성 
		 * @param displayObject
		 * @param viewWidth
		 * @param viewHeight
		 * @param backgroundColor
		 * @param backgroundAlpha
		 * @return 
		 * 
		 */		
		static public function makeXMSprite( 
			displayObject : DisplayObject = null,
			viewWidth : Number = 0,
			viewHeight : Number = 0,
			backgroundColor : uint = 0xffffff,
			backgroundAlpha : int = 1
		):XMSprite
		{
			var xms : XMSprite = new XMSprite();
			xms.backgroundAlpha = backgroundAlpha;
			xms.backgroundColor = backgroundColor;
			xms.viewWidth = viewWidth;
			xms.viewHeight = viewHeight;
			xms.invalidateDisplayList();
			
			if( displayObject != null )	xms.addChild( displayObject );
			
			return xms;
		}
		
		/**
		 * SearchBox 생성
		 * 			예제) var seachBox : TextInput = MakeChildrenUtil.searchBox();
		 * @param width				searchBox 넓이
		 * @param height			searchBox 높이
		 * @param icon_x			searchBox 내부 아이콘 x좌표
		 * @param icon_y			searchBox 내부 아이콘 y표
		 * @return 					TextInput
		 * 
		 */		
		static public function makeSearchBox( 
			width : Number = 440, height : Number = 55,
			icon_x : Number = 13, icon_y : Number = 17 ):TextInput
		{
			var customInputTF : TextInput = new TextInput();
			var icon : MovieClip = new ICON_SEARCH();
			icon.x = icon_x;
			icon.y = icon_y;
			customInputTF.defaultObject = icon;
			customInputTF.bgClip = new BG_TEXT_INPUT;
			customInputTF.padding = [11,11,3,11];
			customInputTF.viewWidth = width;
			customInputTF.viewHeight = height;
			customInputTF.invalidateDisplayList();
			
			return customInputTF;
		}
		
		/**
		 * CheckBox 생성
		 * @param label
		 * @param align
		 * @return 
		 * 
		 */		
		static public function makeCheckBox( label : String , align : String = Align.RIGHT ):CheckBox
		{
			var checkBox : CheckBox = new CheckBox();
			checkBox.backgroundAlpha = 0;
			checkBox.label = label;
			checkBox.alignLabel = align;
			checkBox.invalidateDisplayList();
			
			return checkBox;
		}
		
		/**
		 * CheckGroup 생성
		 * @param parent
		 * @param checkBoxArray
		 * @param arrange
		 * @param gap
		 * @return 
		 * 
		 */		
		static public function makeCheckGroup(parent : DisplayObjectContainer,
			checkBoxArray : Array, arrange : String = Arrange.HORIZONTAL,
			gap : Number = 10
		):CheckGroup
		{
			var checkGroup : CheckGroup = new CheckGroup();
			
			var len : int = checkBoxArray.length;
			var i : int = 0;
			while ( i < len )
			{
				checkGroup.addCheckBox( checkBoxArray[i] );
				i++;
			}
			checkGroup.gapBetweenCheckBoxs = gap;
			checkGroup.arrange = arrange;
			checkGroup.updateDisplayList( parent );
			
			return checkGroup;
		}
		
		/**
		 * RadioBtn 생성
		 * @param label
		 * @param align
		 * @return 
		 * 
		 */		
		static public function makeRadioBtn( label : String , align : String = Align.RIGHT ):RadioButton
		{
			var radioBtn : RadioButton = new RadioButton();
			radioBtn.backgroundAlpha = 0;
			radioBtn.label = label;
			radioBtn.alignLabel = align;
			radioBtn.invalidateDisplayList();
			
			return radioBtn;
		}
		
		/**
		 * RadioGroup 생성
		 * @param parent
		 * @param radioBtnArray
		 * @param arrange
		 * @param gap
		 * @return 
		 * 
		 */		
		static public function makeRadioGroup( parent : DisplayObjectContainer,
			radioBtnArray : Array, arrange : String = Arrange.HORIZONTAL,
			gap : Number = 10
		):RadioGroup
		{
			var radioBtnGroup : RadioGroup = new RadioGroup();
			
			var len : int = radioBtnArray.length;
			var i : int = 0;
			while ( i < len )
			{
				radioBtnGroup.addRadio( radioBtnArray[i] );
				i++;
			}
			radioBtnGroup.gapBetweenRadioButtons = gap;
			radioBtnGroup.arrange = arrange;
			radioBtnGroup.updateDisplayList( parent );
			
			return radioBtnGroup;
		}
		
		/**
		 *							네비게이션 컨텐츠 생성 ( 이전버튼 )
		 * @param naviBtn
		 * @param leftPadding
		 * @return 
		 * 
		 */		
		static public function makeNavigationContent( naviBtn : SWCButton, leftPadding : int = 0 ):NavigationContent
		{
			var nc : NavigationContent = new NavigationContent();
			nc.push( naviBtn );
			nc.paddingLeft = leftPadding;
			naviBtn.addEventListener( MouseEvent.CLICK , onNaviBtnClick );
			
			return nc;
		}
		/**
		 *							이전 버튼 클릭
		 */
		private static function onNaviBtnClick(e:MouseEvent):void
		{
			Navigator.getInstance().removeCurrentView();
		}
		
		
		/**
		 *						타이틀 컨텐츠 생성 
		 * @param titleTextField
		 * @param leftPadding
		 * @param align
		 * @return 
		 * 
		 */		
		static public function makeTitleContent( titleTextField : TextField, leftPadding : int = 20, align : String = Align.LEFT ):TitleContent
		{
			var tc : TitleContent = new TitleContent();
			tc.push( titleTextField );
			tc.paddingLeft = leftPadding;
			tc.align = align;
			
			return tc;
		}
		
		/**
		 *						액션 컨텐츠 생성 
		 * @param displayObject
		 * @param rightPadding
		 * @param gap
		 * @return 
		 * 
		 */		
		static public function makeActionContent( displayObject : DisplayObject, rightPadding : int = 0, gap : int = 10 ):ActionContent
		{
			var ac : ActionContent = new ActionContent();
			ac.push( displayObject );
			ac.paddingRight					= rightPadding;
			ac.gapBetweenContents 			= gap;
			return ac;
		}
		
		/**
		 * 스피너 생성 
		 * @param dataProvider
		 * @param width
		 * @param height
		 * @return 
		 * 
		 */		
		static private function makeSpinner( dataProvider : Array, width : Number, height : Number ):Spinner
		{
			var spinner : Spinner;
			spinner = new Spinner();
			spinner.y = 180;
//			spinner.viewWidth = 300;
//			spinner.viewHeight = 150;
			spinner.dataProvider = dataProvider;
			spinner.invalidateDisplayList();
			
			return spinner;
		}
		
		/**
		 * 스피너 컨테이너 생성 
		 * @param width
		 * @param height
		 * @param dataProvider
		 * @return 
		 * 
		 */		
		static public function makeSpinnerContainer( width : Number, height : Number, ...dataProvider  ):SpinnerContainer
		{
			var spinnerContainer : SpinnerContainer;
			spinnerContainer = new SpinnerContainer();
			spinnerContainer.backgroundAlpha = 1;
			spinnerContainer.backgroundColor = 0x000000;
			spinnerContainer.viewWidth = width;
			spinnerContainer.viewHeight = height;
			
			var len : int = dataProvider.length;
			var i : int = 0;
			var spinner : Spinner;
			while ( i < len )
			{
				spinner = MakeChildrenUtil.makeSpinner( dataProvider[i], 300, 150 );
				spinnerContainer.addSpinner( spinner );
				++i;
			}
			spinnerContainer.invalidateDisplayList();
			
			return spinnerContainer;
		}
		
		/**
		 * 달력 스피너 컨테이너 생성 
		 * @param width
		 * @param height
		 * @param dataProvider
		 * @return 
		 * 
		 */		
		static public function makeCalendarSpinner( width : Number, height : Number, startYear : int = 1900, endYear : int = 2000  ):CalendarSpinner
		{
			// Date
			var date : Date = new Date();
			
			// CalendarSpinner
			var calendarSpinner : CalendarSpinner = new CalendarSpinner();
			calendarSpinner.viewWidth = width;
			calendarSpinner.viewHeight = height;
			
			calendarSpinner.setYear( 1900, 2100, date.fullYear );
			calendarSpinner.setMonth( date.month );
			calendarSpinner.setDate( date.date );
			
			calendarSpinner.invalidateDisplayList();
			
			return calendarSpinner;
		}
		
		/**
		 *  
		 * @param alignType
		 * @param cellSize
		 * @param child
		 * @return 
		 * 
		 */		
		static public function makeAlignFactory( childList : Array, alignType : String = AlignFactory.TYPE_VERTICAL, cellSize : int = 0 ):AlignFactory
		{
			var af : AlignFactory = new AlignFactory(0, null, alignType );
			if( cellSize != 0 )	af.setDefaultCellSize( cellSize );
			var len : int = childList.length;
			var i : int = 0;
			var paddingArray : Array = [];
			
			while ( i < len )
			{
				af.addSimpleItem( childList[i], [0,0,0,0] );
				++i;
			}
			af.workingAlign();
			
			return af;
		}
		
		//constructor 
		public function MakeChildrenUtil(){}
	}
}